//
//  HMOriginalView.swift
//  WeiBo
//
//  Created by Feng on 2017/6/8.
//  Copyright © 2017年 feng. All rights reserved.
//

import UIKit
import SnapKit
import YYText

class HMOriginalView: UIView {
    //设置约束属性
    var originalViewContraint: Constraint?
    
    var statusCellViewModel: HMStatusCellViewModel?
    {
        didSet {
            guard let model = statusCellViewModel else{
                return
            }
            
            if let urlStr = model.status?.user?.profile_image_url
            {
                userImageView.sd_setImage(with: URL(string:urlStr), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            //昵称
            screenNameLabel.text = model.status?.user?.screen_name
            //微博内容
//            contentLabel.text = model.status?.text
            contentLabel.attributedText = model.originalAttributedText
            //会员等级
            mbrankImageView.image = model.mbrankImage
            //认证类型
            verifiedTypeImageView.image = model.verifiedImage
            //来源
            sourceLabel.text = model.sourceTitle
            //微博创建时间
            timeLabel.text = model.createTime
//            print(model.status?.created_at)
            
            //原创微博配图
            //先卸载约束
            originalViewContraint?.deactivate()
            //再判断
            if let picUrls = model.status?.pic_urls, picUrls.count > 0
            {
                picView.isHidden = false
                
                picView.picUrls = picUrls
                
                self.snp.makeConstraints({ (make) in
                    originalViewContraint = make.bottom.equalTo(picView).offset(HomeCellMargin).constraint
                })
            }
            else
            {
                picView.isHidden = true
             
                self.snp.makeConstraints({ (make) in
                    originalViewContraint = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
            }
            
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: --懒加载控件
    //  用户头像
    private lazy var userImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //  认证类型图片
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //  昵称
    private lazy var screenNameLabel: UILabel = {
        let label = UILabel(fontSize: 14, textColor: UIColor.darkGray)
        label.text = "哈哈"
        return label
    }()
    
    //  时间
    private lazy var timeLabel: UILabel = {
        let label = UILabel(fontSize: 12, textColor: UIColor.orange)
        label.text = "刚刚"
        return label
    }()
    
    //  来源
    private lazy var sourceLabel: UILabel = {
        let label = UILabel(fontSize: 12, textColor: UIColor.lightGray)
        label.text = "来自: 新浪微博"
        return label
    }()
    //  会员等级图片
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //  微博内容
//    private lazy var contentLabel: UILabel = {
//        let label = UILabel(fontSize: 14, textColor: UIColor.darkGray)
//        label.text = "窗前明月光，疑似地上霜，举头望明月，低头思故乡‘窗前明月光，疑似地上霜，举头望明月，低头思故乡窗前明月光，疑似地上霜，举头望明月，低头思故乡窗前明月光，疑似地上霜，举头望明月，低头思故乡窗前明月光，疑似地上霜，举头望明月，低头思故乡"
//        //  多行显示
//        label.numberOfLines = 0
//        return label
//    }()
    
    //使用YYText中的YYLabel 创建contentLabel
    private lazy var contentLabel: YYLabel =
    {
        let label = YYLabel()
        //设置最大换行宽度
        label.preferredMaxLayoutWidth = ScreenWidth - 2 * HomeCellMargin
        label.numberOfLines = 0
        return label
    }()
    
    //微博配图
    private lazy var picView: HMPictureView =
    {
        let pictureView = HMPictureView()
        
        return pictureView
    }()
    
    //MARK: --设置界面约束
    func setupUI()
    {
        backgroundColor = UIColor.white
        
        addSubview(userImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(mbrankImageView)
        addSubview(contentLabel)
        addSubview(picView)
        
        picView.backgroundColor = backgroundColor
        
        //  设置约束
        userImageView.snp.makeConstraints { (make) in
            make.top.leading.equalTo(self).offset(HomeCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        verifiedTypeImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(userImageView.snp.trailing)
            make.centerY.equalTo(userImageView.snp.bottom)
        }
        
        screenNameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userImageView)
            make.leading.equalTo(userImageView.snp.trailing).offset(HomeCellMargin)
        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(userImageView)
            make.leading.equalTo(screenNameLabel)
        }
        
        sourceLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp.trailing).offset(HomeCellMargin)
        }
        
        mbrankImageView.snp.makeConstraints { (make) in
            make.top.equalTo(screenNameLabel)
            make.leading.equalTo(screenNameLabel.snp.trailing).offset(HomeCellMargin)
        }
        
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userImageView.snp.bottom).offset(HomeCellMargin)
            make.leading.equalTo(userImageView)
            make.trailing.equalTo(self).offset(-10)
        }
        
        picView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(HomeCellMargin)
            make.leading.equalTo(contentLabel)
            make.size.equalTo(CGSize(width: 100, height: 100)).priority(.high)
        }
        
        //  关键约束， 原创微博的底部等于内容的底部加间距10
        self.snp.makeConstraints { (make) in
            //记录约束属性
            originalViewContraint = make.bottom.equalTo(picView).offset(HomeCellMargin).constraint
        }
        
        //  设置高亮点击闭包
        contentLabel.highlightTapAction = { (containerView, attributedText, range, rect) in
            //  containerView: 点击的视图contentLabel
            //  attributedText: 点击视图的富文本
            //  range: 点击高亮富文本的范围
            //  rect: 点击高亮的区域
            //  获取高亮的富文本
            let subAttributedText = attributedText.attributedSubstring(from: range)
            //  获取对应高亮富文本的字符串
            let text = subAttributedText.string
            print(text)
        }
        
    }
}
